package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppCommentReply;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 评论回复
 */
@Repository
public class AppCommentReplyDao extends BaseDao<AppCommentReply> {

    /**
     * 获取指定评论的回复总数量
     *
     * @param commentid
     * @return
     */
    public List<AppCommentReply> hfcount(Integer commentid) {
        if(commentid==null || commentid<=0){
            return null;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment_reply where commentid = ? and status = 1 ");
        paramList.add(commentid);
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppCommentReply.class);
    }


    /**
     * 查询列表
     */
    public List<AppCommentReply> list(Map<String, Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment_reply where 1=1  ");

        if (StringEx.isNotNull(paraMap.get("commentid"))) {
            sbStr.append(" and commentid = ? ");
            paramList.add(paraMap.get("commentid"));
        }

        if (StringEx.isNotNull(paraMap.get("nick"))) {
            sbStr.append(" and nick = ? ");
            paramList.add(paraMap.get("nick"));
        }

        if (StringEx.isNotNull(paraMap.get("phone"))) {
            sbStr.append(" and phone = ? ");
            paramList.add(paraMap.get("phone"));
        }


        if (StringEx.isNotNull(paraMap.get("replynick"))) {
            sbStr.append(" and replynick = ? ");
            paramList.add(paraMap.get("replynick"));
        }

        if (StringEx.isNotNull(paraMap.get("replyphone"))) {
            sbStr.append(" and replyphone = ? ");
            paramList.add(paraMap.get("replyphone"));
        }
        if (StringEx.isNotNull(paraMap.get("status"))) {
            sbStr.append(" and status = ? ");
            paramList.add(paraMap.get("status"));
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppCommentReply.class, splitPage);
    }


    /**
     * 查询列表
     */
    public List<AppCommentReply> specificUserList(Integer commentid, SplitPageBean splitPage) {
        if(commentid==null || commentid<=0){
            return null;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment_reply where commentid = ? and status = 1  ");
        paramList.add(commentid);
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppCommentReply.class, splitPage);
    }


    /**
     * 详情
     */
    public AppCommentReply get(Integer id) {
        if (id==null || id<=0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_comment_reply where id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppCommentReply.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppCommentReply haoYou) {
        if (haoYou!=null && haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id==null || id <= 0) {
            return;
        }
        String sql = "delete  from app_comment_reply where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete  from app_comment_reply where userid = " + userId + " or replyid=" + userId;
        this.del(sql, new ArrayList<>());
    }

    public void updateStatus(Integer id, Integer status) {
        if (id==null || id <= 0 || status==null || status<=0) {
            return;
        }
        String sql = "update app_comment_reply set status = ?  where id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(status);
        paramList.add(id);
        this.update(sql, paramList);
    }


    /**
     * 更新回复人用户头像
     *
     * @param userid
     * @param pic    头像地址
     */
    public void updateImg(Integer userid, String pic) {
        if (userid==null || userid<=0 || StringEx.isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_comment_reply SET pic = ?  WHERE userid = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新回复人用户昵称
     *
     * @param id
     * @param nick 昵称
     */
    public void updateNick(Integer id, String nick) {
        if (id==null || id<=0 || StringEx.isNull(nick)) {
            return;
        }
        String sql = " UPDATE app_comment_reply SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(id);
        this.update(sql, paramList);
    }


    /**
     * 更新被回复人用户头像
     *
     * @param userid
     * @param pic    头像地址
     */
    public void updateReplyImg(Integer userid, String pic) {
        if (userid==null || userid<=0 || StringEx.isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_comment_reply  SET replypic = ?  WHERE replyid = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新被回复人用户昵称
     *
     * @param id
     * @param nick 昵称
     */
    public void updateReplyNick(Integer id, String nick) {
        if (id==null || id<=0 || StringEx.isNull(nick)) {
            return;
        }
        String sql = " UPDATE app_comment_reply  SET replynick = ?  WHERE replyid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(id);
        this.update(sql, paramList);
    }

}
